/*
 * AddressDao.java
 * 作用：数据访问对象（DAO），负责与数据库中的 `addresses` 表进行交互，提供地址的增删改查操作。
 */
package com.campustradingwall.dao;

import com.campustradingwall.model.Address;
import com.campustradingwall.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AddressDao {

    /**
     * 根据用户ID获取其所有收货地址
     * @param userId 用户ID
     * @return 地址列表
     */
    public List<Address> getAddressesByUserId(int userId) {
        List<Address> addresses = new ArrayList<>();
        String sql = "SELECT * FROM addresses WHERE user_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, userId);
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    addresses.add(extractAddressFromResultSet(rs));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return addresses;
    }

    /**
     * 根据用户ID获取其默认收货地址
     * @param userId 用户ID
     * @return 默认地址，如果没有则返回null
     */
    public Address getDefaultAddressByUserId(int userId) {
        String sql = "SELECT * FROM addresses WHERE user_id = ? AND is_default = 1";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, userId);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    return extractAddressFromResultSet(rs);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 添加新地址
     * @param address 地址对象
     */
    public void addAddress(Address address) {
        String sql = "INSERT INTO addresses (user_id, address, is_default) VALUES (?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, address.getUserId());
            stmt.setString(2, address.getAddress());
            stmt.setBoolean(3, address.isDefault());
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新地址
     * @param address 地址对象
     */
    public void updateAddress(Address address) {
        String sql = "UPDATE addresses SET address = ?, is_default = ? WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, address.getAddress());
            stmt.setBoolean(2, address.isDefault());
            stmt.setInt(3, address.getId());
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除地址
     * @param addressId 地址ID
     */
    public void deleteAddress(int addressId) {
        String sql = "DELETE FROM addresses WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, addressId);
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    private Address extractAddressFromResultSet(ResultSet rs) throws SQLException {
        Address address = new Address();
        address.setId(rs.getInt("id"));
        address.setUserId(rs.getInt("user_id"));
        address.setAddress(rs.getString("address"));
        address.setDefault(rs.getBoolean("is_default"));
        return address;
    }
} 